package com.rstrulak.managedBeans.controllers;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.NoteAttachmentsDao;
import com.rstrulak.dao.NoteDao;
import com.rstrulak.dao.ProjectAttachmentsDao;
import com.rstrulak.dao.ProjectDao;
import com.rstrulak.dao.UserDao;
import com.rstrulak.dao.UserProjectDao;
import com.rstrulak.model.Note;
import com.rstrulak.model.NoteAttachments;
import com.rstrulak.model.Project;
import com.rstrulak.model.ProjectAttachments;
import com.rstrulak.model.User;
import com.rstrulak.model.UserProject;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.FileUtils;
import com.rstrulak.utils.JSFUtils;

@Component("newProjectFNBean")
@Scope("request")
public class NewProjectFromNoteBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -945358760825549817L;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	UserDao userDao;

	@Autowired
	UserProjectDao userProjectDao;
	
	@Autowired
	NoteDao noteDao;
	
	@Autowired
	private NoteAttachmentsDao noteAttachmentsDao;
	
	@Autowired
	private ProjectAttachmentsDao projectAttachmentsDao;

	private String projectName;

	private String projectDescription;

	private Note note;

	private Long noteId;

	private Project project;

	private UserProject userProject;
	
	private Map<String, Long> allUsersMap;

	private List<String> assignedUsers;
	
	private List<NoteAttachments> noteAttachmentsList;
	
	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
		getNote();
		initializeMap();
	}

	private void initializeMap() {
		List<User> allUsersExceptUser = userDao.getAll();
		allUsersMap = new HashMap<String, Long>();

		for (User user : allUsersExceptUser) {
			allUsersMap.put(user.getFirstName() + " " + user.getLastName(), user.getId());
		}
	}

	private void getNote() {
		noteId = (Long) JSFUtils.flashScope().get(Constants.NOTE_ID);
		JSFUtils.flashScope().put(Constants.NOTE_ID, noteId);
		
		getNoteFromDb(noteId);

		if (note != null) {
			noteAttachmentsList = noteAttachmentsDao.getNoteAttachemntsListByNote(note);
		}
		intializeField();

	}

	private void intializeField() {
		projectName = note.getName();
		projectDescription = note.getDescription();
	}

	private void getNoteFromDb(Long noteId) {
		note = noteDao.getById(noteId);

	}
	
	public Map<String, Long> getAllUsersMap() {
		return allUsersMap;
	}

	public void setAllUsersMap(Map<String, Long> allUsersMap) {
		this.allUsersMap = allUsersMap;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	
	public String getProjectDescription() {
		return projectDescription;
	}

	public void setProjectDescription(String projectDescription) {
		this.projectDescription = projectDescription;
	}
	
	public List<String> getAssignedUsers() {
		return assignedUsers;
	}

	public void setAssignedUsers(List<String> assignedUsers) {
		this.assignedUsers = assignedUsers;
	}
	
	public List<NoteAttachments> getNoteAttachmentsList() {
		return noteAttachmentsList;
	}

	public void setNoteAttachmentsList(List<NoteAttachments> noteAttachmentsList) {
		this.noteAttachmentsList = noteAttachmentsList;
	}

	public void saveProject() throws ServletException, IOException {
		project = new Project();
		project.setDescription(projectDescription);
		project.setName(projectName);
		project.setOwner(JSFUtils.getLoggedUser());
		Long projectId = projectDao.save(project);

		userProject = new UserProject();
		userProject.setUser(JSFUtils.getLoggedUser());
		userProject.setProject(project);
		userProjectDao.save(userProject);

		for (String userId : assignedUsers) {
			userProject = new UserProject();
			userProject.setUser(userDao.getById(new Long(userId)));
			userProject.setProject(project);
			userProjectDao.save(userProject);
		}
		note.setIsFinished(Boolean.TRUE);
		noteDao.saveOrUpdate(note);
		
		for (NoteAttachments noteAttachments : noteAttachmentsList) {
			if (noteAttachments.getIsChecked()) {
				ProjectAttachments projectAttachments = new ProjectAttachments();
				StringBuilder finalName = new StringBuilder();
				finalName.append("projectId_");
				finalName.append(projectId);
				finalName.append("_fileName_");
				finalName.append(noteAttachments.getFileName());

				projectAttachments.setProject(project);
				String newPath = Constants.PROJECT_ATTACHMENTS_PATH + finalName;
				projectAttachments.setFilePath(newPath);
				projectAttachments.setFileName(noteAttachments.getFileName());
				projectAttachmentsDao.save(projectAttachments);
				
				FileUtils.copyfile(noteAttachments.getFilePath(), newPath);
				File toDelete = new File(noteAttachments.getFilePath());
				toDelete.delete();

			}
		}

		JSFUtils.redirect("/protected/note.xhtml");
	}

	public void cancelAddProject() throws ServletException, IOException {
		JSFUtils.redirect("/protected/note.xhtml");
	}

}
